package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.stu.server.entity.*;
import com.xz.stu.server.service.*;
import com.xz.stu.server.mapper.CourseMapper;
import com.xz.stu.server.vo.course.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 熊磊
* @description 针对表【school_course】的数据库操作Service实现
* @createDate 2025-03-01 15:18:50
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{
    @Resource
    private DepartmentService departmentService;
    @Resource
    private CourseMapper courseMapper;
    @Override
    public Long createCourse(CourseCreateReqVO createReqVO) {
        Course course = BeanUtils.toBean(createReqVO, Course.class);
        this.save(course);
        return course.getId();
    }

    @Override
    public void updateCourse(CourseUpdateReqVO updateReqVO) {
        Course course = BeanUtils.toBean(updateReqVO, Course.class);
        this.updateById(course);
    }

    @Override
    public void deleteCourse(List<Long> courseId) {
        this.removeByIds(courseId);
    }

    @Override
    public CourseRespVO getCourse(Long courseId) {
        Course course = this.getById(courseId);
        if (course != null) {
            CourseRespVO courseRespVO = BeanUtils.toBean(course, CourseRespVO.class);
            Department department = departmentService.getById(course.getDeptId());
            courseRespVO.setDeptName(department.getDeptName());
            return courseRespVO;
        }
        return null;
    }


    @Override
    public List<CourseRespVO> getCourseList(CourseListReqVO listReqVO) {
        Map<Long, String> deptMap = departmentService.list().stream().collect(Collectors.toMap(Department::getId, Department::getDeptName));
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(listReqVO.getCourseId()), Course::getCourseId, listReqVO.getCourseId())
                .like(Objects.nonNull(listReqVO.getCourseName()), Course::getCourseName, listReqVO.getCourseName())
                .eq(!Objects.isNull(listReqVO.getDeptId()), Course::getDeptId, listReqVO.getDeptId());

        List<Course> courses = this.list(queryWrapper);
        return courses.stream().map(course -> {
            CourseRespVO courseRespVO = BeanUtils.toBean(course, CourseRespVO.class);
            courseRespVO.setDeptName(deptMap.get(course.getDeptId()));
            return courseRespVO;
        }).collect(Collectors.toList());

    }

    @Override
    public PageResult<CourseRespVO> getCourseListPage(CourseListPageReqVO listReqVO) {
        Map<Long, String> deptMap = departmentService.list().stream().collect(Collectors.toMap(Department::getId, Department::getDeptName));
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Objects.nonNull(listReqVO.getCourseId()), Course::getCourseId, listReqVO.getCourseId())
                .like(Objects.nonNull(listReqVO.getCourseName()), Course::getCourseName, listReqVO.getCourseName())
                .eq(!Objects.isNull(listReqVO.getDeptId()), Course::getDeptId, listReqVO.getDeptId());

        Page<Course> page = this.page(new Page<>(listReqVO.getPageNo(), listReqVO.getPageSize()), queryWrapper);
        List<CourseRespVO> list = page.getRecords().stream().map(course -> {
            CourseRespVO courseRespVO = BeanUtils.toBean(course, CourseRespVO.class);
            courseRespVO.setDeptName(deptMap.get(course.getDeptId()));
            return courseRespVO;
        }).collect(Collectors.toList());

        return new PageResult<>(list, page.getTotal());

    }

    @Override
    public List<ScheduleRespVO> getCourseListSchedule(Integer week,Long termId) {
        //获取学生ID
        //获取登录用户ID
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        if (userId == null){
            throw new RuntimeException("用户未登录");
        }
        List<ScheduleRespVO> courseListSchedule = courseMapper.getCourseListSchedule(userId, week,termId);
        return courseListSchedule;

    }
}




